package com.nutrition.health.repository;

import com.nutrition.health.model.FoodItem;
import com.nutrition.health.model.FoodItem.FoodCategory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface FoodItemRepository extends JpaRepository<FoodItem, Long> {
    
    Optional<FoodItem> findByBarcode(String barcode);
    
    List<FoodItem> findByCategory(FoodCategory category);
    
    Page<FoodItem> findByNameContainingIgnoreCase(String name, Pageable pageable);
    
    @Query("SELECT f FROM FoodItem f WHERE " +
           "LOWER(f.name) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
           "LOWER(f.description) LIKE LOWER(CONCAT('%', :keyword, '%'))")
    Page<FoodItem> searchByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    @Query("SELECT f FROM FoodItem f JOIN NutritionInfo n ON f.id = n.foodItemId WHERE " +
           "n.calories <= :maxCalories")
    List<FoodItem> findByMaxCalories(@Param("maxCalories") Double maxCalories);
    
    @Query("SELECT f FROM FoodItem f JOIN NutritionInfo n ON f.id = n.foodItemId WHERE " +
           "n.protein >= :minProtein")
    List<FoodItem> findByMinProtein(@Param("minProtein") Double minProtein);
}